﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetCore8Demo.Models.PLC;
using NetCore8Demo.Tool;
using Newtonsoft.Json;

namespace NetCore8Demo.Models.Communication.SerialPortFolder
{
    internal class StaticSerialPort
    {
        public static string SavePath = "Communication";
        public static string SaveName = "SerialPortInfoList";


        public static List<int> BoundList =
        [
            9600,
            115200,
            1382400,
            921600,
            460800,
            256000,
            230400,
            128000,
            76800,
            57600,
            43000,
            38400,
            19200,
            14400,
            4800,
            1200
        ];

        public static List<int> DataBitList = [8,7,6,5];


        public static List<SerialPortInfo> SerialPortInfoList = new();

        public static List<string> GetAllPortName()
        {
           return SerialPort.GetPortNames().ToList();
         
        }


        public static void InitStaticSerialPortInfoList()
        {
            SerialPortInfoList = JsonParam.LoadParamPath<List<SerialPortInfo>>(SavePath, SaveName) ??
                                 new List<SerialPortInfo>();
        }

        public static List<SerialPortInfo> LoadSerialPortInfoList()
        {
            List<SerialPortInfo>  serialPortInfoList =
                JsonParam.LoadParamPath<List<SerialPortInfo>>(SavePath, SaveName) ??
                new List<SerialPortInfo>();
            return serialPortInfoList;

        }

        public static bool SavaSerialPortInfoList(List<SerialPortInfo> serialPortInfoList)
        {
            return JsonParam.SaveParamPath(SavePath, SaveName, serialPortInfoList);

        }



    }




    public class SerialPortInfo
    {
        public Guid Id;
        public int Index;
        public string Name = "";

        public string PortName = "";
        public int BaudRate = 9600;
        public int DataBits=8;
        public StopBits StopBits = StopBits.None;
        public Parity Parity = Parity.None;

        public bool IsInUse = true;
        public SerialPortDataType DataType = SerialPortDataType.Ascii;
        public string SendData="";
        public bool IsWaitData;

       

    }

    public enum SerialPortDataType
    {
        Ascii,
        Hex,
    }

    public enum YesOrNoEnum
    {
        是,
        否,
    }



}
